package ai.tuobot.sdk.util

import ai.tuobot.sdk.model.AudioConfig
import org.json.JSONException
import org.json.JSONObject

internal object ConfigToObj {
    fun cfgJsonToAudioConfig(cfgJson: JSONObject?): AudioConfig {
        cfgJson?.let {
            if (it.has("audio")) {
                val audioConfig = AudioConfig()
                try {
                    var autoPlay = audioConfig.auto_play
                    var volumeThreshold = audioConfig.volume_threshold
                    var endListenSilenceTimeOfWakeup= audioConfig.end_listen_silence_time_of_wakeup
                    var endListenSilencetime = audioConfig.end_listen_silence_time
                    var sampleRate = audioConfig.sampleRate
                    var breakLLMSay = audioConfig.break_llm_say
                    var format = audioConfig.format
                    var ANS = audioConfig.ANS
                    var VAD = audioConfig.VAD
                    var internalAEC = audioConfig.internalAEC


                    val audioObj = it.getJSONObject("audio")
                    if (audioObj.has("auto_play"))
                        autoPlay = audioObj.getBoolean("auto_play")
                    if (audioObj.has("volume_threshold"))
                        volumeThreshold = audioObj.getInt("volume_threshold")
                    if (audioObj.has("end_listen_silence_time_of_wakeup"))
                        endListenSilenceTimeOfWakeup = audioObj.getInt("end_listen_silence_time_of_wakeup")
                    if (audioObj.has("end_listen_silence_time"))
                        endListenSilencetime = audioObj.getInt("end_listen_silence_time")
                    // 本地STT只支持16K采样
                    /*if (audioObj.has("sample_rate"))
                        sampleRate = audioObj.getInt("sample_rate")*/
                    if (audioObj.has("break_llm_say"))
                        breakLLMSay = audioObj.getBoolean("break_llm_say")
                    // 本地STT只支持WAV
                    /*if (audioObj.has("format"))
                        format = audioObj.getString("format")*/
                    if (audioObj.has("ANS"))
                        ANS = audioObj.getBoolean("ANS")
                    if (audioObj.has("VAD"))
                        VAD = audioObj.getBoolean("VAD")
                    if (audioObj.has("internal_aec"))
                        internalAEC = audioObj.getBoolean("internal_aec")

                    val newAudioConfig = AudioConfig(
                        autoPlay,
                        volumeThreshold,
                        endListenSilenceTimeOfWakeup,
                        endListenSilencetime,
                        breakLLMSay,
                        format,
                        ANS,
                        VAD,
                        sampleRate,
                        internalAEC
                    )
                    return newAudioConfig
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
            }
        }
        return AudioConfig()
    }

}